package com.ruyuan.eshop.construction.api.impl;

import com.alibaba.fastjson.JSON;
import com.ruyuan.eshop.common.core.JsonResult;
import com.ruyuan.eshop.common.core.PageResult;
import com.ruyuan.eshop.construction.api.AttributeApi;
import com.ruyuan.eshop.construction.domain.dto.AttributeDetailDTO;
import com.ruyuan.eshop.construction.domain.dto.AttributeResultDTO;
import com.ruyuan.eshop.construction.domain.request.QueryAttributeRequest;
import com.ruyuan.eshop.construction.domain.request.AttributeRequest;
import com.ruyuan.eshop.common.exception.ProductBizException;
import com.ruyuan.eshop.construction.service.AttributeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 规格服务
 *
 * @author zhonghuashishan
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = AttributeApi.class, retries = 0)
public class AttributeApiImpl implements AttributeApi {

    @Autowired
    private AttributeService attributeService;


    @Override
    public JsonResult<AttributeResultDTO> saveAttribute(AttributeRequest request) {
        try {
            AttributeResultDTO attributeResultDTO =
                    attributeService.saveAttribute(request);
            return JsonResult.buildSuccess(attributeResultDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }


    @Override
    public JsonResult<AttributeResultDTO> updateAttribute(AttributeRequest request) {
        try {
            AttributeResultDTO attributeResultDTO =
                    attributeService.updateAttribute(request);
            return JsonResult.buildSuccess(attributeResultDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<PageResult<AttributeDetailDTO>> pageAttribute(QueryAttributeRequest request) {
        try {
            PageResult<AttributeDetailDTO> attributePageResult = attributeService.pageAttribute(request);
            return JsonResult.buildSuccess(attributePageResult);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
